package cn.com.picc.asr;
import com.iflytek.ai.platform.api.core.entity.ApiResponse;
import com.iflytek.ai.platform.api.sdk.asr.client.IatApiClient;
import com.iflytek.ai.platform.api.sdk.asr.entity.OnlineIatRequest;
import com.iflytek.ai.platform.api.sdk.asr.entity.OnlineIatResponse;
import com.iflytek.ai.platform.api.sdk.asr.listener.OnlineIatListener;
import com.iflytek.ai.platform.api.sdk.common.entity.AppCredential;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

/**
 * @author 曹晟
 * @version v1.00.00
 * @className ASRClient
 * @description ASR模型调用客户端
 * @date 2025/03/28
 **/
public class ASRClient {

    protected IatApiClient client;

    public ASRClient(String url,
                     String appId,
                     String abilityId,
                     String secretId,
                     String secretKey) {
        // 应用凭证
        AppCredential credential = new AppCredential(secretId, secretKey, appId);
        // 客户端
        IatApiClient client = new IatApiClient(credential, abilityId, url);
        this.client = client;
    }

    public String invoke(String file,
                         int rate,
                         int bits,
                         int timeout)  throws Exception {
        return invoke(new FileInputStream(file), rate, bits, timeout);
    }

    public String invoke(byte[] bytes,
                         int rate,
                         int bits,
                         int timeout)  throws Exception {
        return invoke(new ByteArrayInputStream(bytes), rate, bits, timeout);
    }

    public String invoke(InputStream audioStream,
                         int rate,
                         int bits,
                         int timeout)  throws Exception {

        // 构造请求体
        final OnlineIatRequest request = new OnlineIatRequest();
        //相关参数参考SDK文档
        request.setRate(rate);
        request.setBits(bits);
        request.setAudioStream(audioStream);
        final String[] res = {""};
        final boolean[] finish = {false};
        // 创建WS连接返回处理对象
        client.newSpeechRecognizer(request, new OnlineIatListener() {
            @Override
            public void onMessage(OnlineIatResponse result) {
                res[0] += result.getContent();
            }

            @Override
            public void onComplete(ApiResponse response) {
                // 会话关闭处理
                finish[0] = true;
            }

            @Override
            public void onFail(ApiResponse response) {
                System.out.println("Error when use asr model:" + response.toString());
                res[0] = "Error when use asr model:" + response.getMessage();
                finish[0] = true;
            }

        });
        for (int i = 0; i < timeout; i++) {
            if (finish[0]) {
                break;
            }
            TimeUnit.SECONDS.sleep(1);
        }
        return res[0];
    }

}
